import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 加减法口算练习系统-v0.3
 * 改进功能：避免重复算式、控制加减法比例
 */
public class CalculationSystemV03 {
    public static void main(String[] args) {
        Random random = new Random();
        int totalQuestions = 50;
        int problemsPerLine = 5;

        // 控制加减法比例：60%加法，40%减法
        double additionRatio = 0.6;

        // 使用Set来存储已生成的算式，避免重复
        Set<String> generatedExpressions = new HashSet<>();

        System.out.println("加减法口算练习系统-v0.3");
        System.out.println("========================");

        int questionCount = 0;
        int attempts = 0; // 防止无限循环
        final int MAX_ATTEMPTS = 1000;

        while (questionCount < totalQuestions && attempts < MAX_ATTEMPTS) {
            attempts++;
            int num1, num2, result;
            String operator;
            boolean isAddition;

            // 根据比例决定生成加法还是减法
            if (random.nextDouble() < additionRatio) {
                // 生成加法算式 - 修复边界问题
                isAddition = true;
                num1 = random.nextInt(100) + 1; // 1-100
                // 确保num2的范围是1到(100-num1)，避免bound为0或负数
                int maxNum2 = 100 - num1;
                if (maxNum2 <= 0) {
                    continue; // 重新生成
                }
                num2 = random.nextInt(maxNum2) + 1; // 确保和不超过100
                result = num1 + num2;
                operator = "+";
            } else {
                // 生成减法算式
                isAddition = false;
                num2 = random.nextInt(100) + 1; // 1-100
                // 确保被减数大于等于减数，且不超过100
                num1 = random.nextInt(100) + 1;
                if (num1 < num2) {
                    // 如果num1小于num2，交换它们
                    int temp = num1;
                    num1 = num2;
                    num2 = temp;
                }
                result = num1 - num2;
                operator = "-";
            }

            // 创建算式的唯一标识
            String expressionKey = num1 + operator + num2;

            // 检查是否重复
            if (!generatedExpressions.contains(expressionKey)) {
                generatedExpressions.add(expressionKey);
                questionCount++;

                // 构建算式字符串
                String expression = String.format("%2d %s %2d = %-3d",
                        num1, operator, num2, result);

                // 输出算式
                System.out.print(" " + expression);

                // 每行结束时换行
                if (questionCount % problemsPerLine == 0) {
                    System.out.println();
                }
            }
        }

        if (attempts >= MAX_ATTEMPTS) {
            System.out.println("\n警告：达到最大尝试次数，可能无法生成更多不重复的题目");
        }

        // 统计信息
        System.out.println("\n========================");
        System.out.println("习题生成完成！");
        System.out.println("加法算式: " + countAdditionExpressions(generatedExpressions));
        System.out.println("减法算式: " + countSubtractionExpressions(generatedExpressions));
        System.out.println("总题数: " + questionCount);
        System.out.println("尝试次数: " + attempts);
    }

    // 统计加法算式数量
    private static int countAdditionExpressions(Set<String> expressions) {
        int count = 0;
        for (String expr : expressions) {
            if (expr.contains("+")) {
                count++;
            }
        }
        return count;
    }

    // 统计减法算式数量
    private static int countSubtractionExpressions(Set<String> expressions) {
        int count = 0;
        for (String expr : expressions) {
            if (expr.contains("-")) {
                count++;
            }
        }
        return count;
    }
}